# coding: utf-8
from django import forms
from django.forms import ModelForm
from themes.models import *
from django.forms import widgets

JOIN_CHOICES = (
    ( '1', '需要审核' ),
    ( '0', '不需要审核' ),
)

class AddThemeForm( ModelForm ):
    class Meta:
        model = Theme
        exclude = ( 'member_count', 'point', 'notice', 'user', 'users' )
    def __init__( self, *args, **kwargs ):
        self.user = kwargs.pop( 'user', None )
        super( AddThemeForm, self ).__init__( *args, **kwargs )
    def save( self, commit = True ):
        theme = Theme( user = self.user,
                       name = self.cleaned_data['name'],
                       people = self.cleaned_data['people'],
                       intro = self.cleaned_data['intro'],
                       objective = self.cleaned_data['objective'],
                       strategy = self.cleaned_data['strategy'],
                       content = self.cleaned_data['content'],
                       join = self.cleaned_data['join'], )
        user = self.user
        if commit:
            theme.save()
        return theme

class EditThemeForm( ModelForm ):
    class Meta:
         model = Theme
         exclude = ( 'member_count', 'point', 'notice', 'user', 'users' )
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop( 'user', None )
        super( EditThemeForm, self).__init__(*args, **kwargs)
        self.fields['name'].widget = forms.TextInput()
        self.fields['people'].widget = forms.Textarea()
        self.fields['intro'].widget = forms.Textarea()
        self.fields['objective'].widget = forms.Textarea()
        self.fields['strategy'].widget = forms.Textarea()
        self.fields['content'].widget = forms.Textarea()
        self.fields['pic'].widget = forms.FileInput()
        self.fields['join'].widget = forms.Select( choices = JOIN_CHOICES )
    def save( self, commit = True ):
        theme = super( EditThemeForm, self ).save( commit = False )
        user = self.user
        if commit:
            theme.save()
        return theme

class AddPostForm( forms.ModelForm ):
    name = forms.CharField( label = '标题', max_length = 255,
                           widget = forms.TextInput( attrs = { 'size':'115' } ) )

    class Meta:
        model = Post
        fields = ( 'name', 'content' )

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop( 'user', None )
        self.topic = kwargs.pop( 'topic', None )
        self.theme = kwargs.pop( 'theme', None )
        super(AddPostForm, self).__init__(*args, **kwargs)
        if self.topic:
            self.fields['name'].widget = forms.HiddenInput()
            self.fields['name'].required = False
        self.fields['content'].widget = forms.Textarea( attrs={ 'rows':'20', 'cols':'95' } )

    def save( self ):
        if self.theme:
            topic = Topic( theme = self.theme,
                           user = self.user,
                           name = self.cleaned_data['name'] )
            topic.save()
        else:
            topic = self.topic
        post = Post( topic = topic,
                     user = self.user,
                     content = self.cleaned_data['content'] )
        post.save()
        return post

